// ExceptionCFG.cpp : implementation file
//

#include "stdafx.h"
#include "main.h"
#include "ExceptionCFG.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CExceptionCFG dialog


CExceptionCFG::CExceptionCFG(CWnd* pParent /*=NULL*/)
	: CDialog(CExceptionCFG::IDD, pParent)
{
	//{{AFX_DATA_INIT(CExceptionCFG)
	m_bHandleType1 = FALSE;
	m_bHandleType2 = FALSE;
	m_bHandleType3 = FALSE;
	m_bHandleType4 = FALSE;
	m_bAlarmOut1 = FALSE;
	m_bAlarmOut2 = FALSE;
	m_bAlarmOut3 = FALSE;
	m_bAlarmOut4 = FALSE;
	//}}AFX_DATA_INIT
}


void CExceptionCFG::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExceptionCFG)
	DDX_Control(pDX, IDC_COMEXCEPTION, m_ExceptionCtrl);
	DDX_Check(pDX, IDC_CHECKHANDLETYPE1, m_bHandleType1);
	DDX_Check(pDX, IDC_CHECKHANDLETYPE2, m_bHandleType2);
	DDX_Check(pDX, IDC_CHECKHANDLETYPE3, m_bHandleType3);
	DDX_Check(pDX, IDC_CHECKHANDLETYPE4, m_bHandleType4);
	DDX_Check(pDX, IDC_CHKALARMOUT1, m_bAlarmOut1);
	DDX_Check(pDX, IDC_CHKALARMOUT2, m_bAlarmOut2);
	DDX_Check(pDX, IDC_CHKALARMOUT3, m_bAlarmOut3);
	DDX_Check(pDX, IDC_CHKALARMOUT4, m_bAlarmOut4);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CExceptionCFG, CDialog)
	//{{AFX_MSG_MAP(CExceptionCFG)
	ON_BN_CLICKED(IDC_BUTOK, OnButok)
	ON_BN_CLICKED(IDC_BUTEXIT, OnButexit)
	ON_BN_CLICKED(IDC_CHECKHANDLETYPE4, OnCheckhandletype4)
	ON_CBN_SELCHANGE(IDC_COMEXCEPTION, OnSelchangeComexception)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExceptionCFG message handlers

BOOL CExceptionCFG::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CRect rc(0, 0, 0, 0);
	GetParent()->GetClientRect(&rc);
	((CTabCtrl*)GetParent())->AdjustRect(FALSE, &rc);
	MoveWindow(&rc);
	
	DWORD dwReturned;
	CString sTemp;
	m_SelType = 0;
	m_ExceptionCtrl.SetCurSel(m_SelType);
	if (m_bConnectServer)
	{
		if (!NET_DVR_GetDVRConfig(m_lServerID, NET_DVR_GET_EXCEPTIONCFG, 0,&m_ExceptionInfo, sizeof(NET_DVR_EXCEPTION), &dwReturned))
		{
			sTemp.Format("ERROR: NET_DVR_GET_EXCEPTIONCFG = %d \n", NET_DVR_GetLastError());
			AfxMessageBox(sTemp);	
		}
		else
		{
			m_bHandleType1 = m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType &0x01;
			m_bHandleType2 = (m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType>>1)&0x01;
			m_bHandleType3 = (m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType>>2)&0x01;
			m_bHandleType4 = (m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType>>3)&0x01;
			EnableControl(m_bHandleType4);
		}
	}
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CExceptionCFG::OnCancel() 
{
	// TODO: Add extra cleanup here
	
//	CDialog::OnCancel();
}

void CExceptionCFG::OnOK() 
{
// TODO: Add extra validation here
	
//	CDialog::OnOK();
}

void CExceptionCFG::OnButok() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_SelType = m_ExceptionCtrl.GetCurSel();
	m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType = 0;
	m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType |= (m_bHandleType1 << 0);
	m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType |= (m_bHandleType2 << 1);
	m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType |= (m_bHandleType3 << 2);
	m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType |= (m_bHandleType4 << 3);
	m_ExceptionInfo.struExceptionHandleType[m_SelType].byRelAlarmOut[0] = m_bAlarmOut1;
	m_ExceptionInfo.struExceptionHandleType[m_SelType].byRelAlarmOut[1] = m_bAlarmOut2;
	m_ExceptionInfo.struExceptionHandleType[m_SelType].byRelAlarmOut[2] = m_bAlarmOut3;
	m_ExceptionInfo.struExceptionHandleType[m_SelType].byRelAlarmOut[3] = m_bAlarmOut4;
	
	m_bSetPara = TRUE;
}

void CExceptionCFG::OnButexit() 
{
	// TODO: Add your control notification handler code here
	m_bSetPara = FALSE;
}

void CExceptionCFG::OnCheckhandletype4() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	EnableControl(m_bHandleType4);
}

void CExceptionCFG::OnSelchangeComexception() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_SelType = m_ExceptionCtrl.GetCurSel();
	m_bHandleType1 = m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType &0x01;
	m_bHandleType2 = (m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType>>1)&0x01;
	m_bHandleType3 = (m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType>>2)&0x01;
	m_bHandleType4 = (m_ExceptionInfo.struExceptionHandleType[m_SelType].dwHandleType>>3)&0x01;
	EnableControl(m_bHandleType4);
	
	UpdateData(FALSE);
}

void CExceptionCFG::EnableControl(BOOL bEnable)
{
	if (m_dwAlarmOutNum >= 1)
	{
		GetDlgItem(IDC_CHKALARMOUT1)->EnableWindow(bEnable);
		m_bAlarmOut1 = m_ExceptionInfo.struExceptionHandleType[m_SelType].byRelAlarmOut[0];
	}
	if (m_dwAlarmOutNum >= 2)
	{
		GetDlgItem(IDC_CHKALARMOUT2)->EnableWindow(bEnable);
		m_bAlarmOut2 = m_ExceptionInfo.struExceptionHandleType[m_SelType].byRelAlarmOut[1];
	}
	if (m_dwAlarmOutNum >= 3)
	{
		GetDlgItem(IDC_CHKALARMOUT3)->EnableWindow(bEnable);
		m_bAlarmOut3 = m_ExceptionInfo.struExceptionHandleType[m_SelType].byRelAlarmOut[2];
	}
	if (m_dwAlarmOutNum >= 4)
	{
		GetDlgItem(IDC_CHKALARMOUT4)->EnableWindow(bEnable);
		m_bAlarmOut4 = m_ExceptionInfo.struExceptionHandleType[m_SelType].byRelAlarmOut[3];
	}
}
